IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_InOut_Update_Status')
	DROP PROCEDURE dbo.sp_InOut_Update_Status
GO
CREATE PROCEDURE dbo.sp_InOut_Update_Status
	@InOutID INT
AS
BEGIN
	SET NOCOUNT OFF

	DECLARE @_InOutID INT 
	SET @_InOutID = @InOutID

	DECLARE @_TotalLine INT
	DECLARE @_TotalStatus INT

	SELECT 
		@_TotalLine = COUNT(1)
		, @_TotalStatus = SUM(CASE Status WHEN 'N' THEN 1 WHEN 'C' THEN 2 END)
	FROM 
		dbo.InOutLines 
	WHERE 
		InOutID = @_InOutID

	UPDATE dbo.InOuts 
	SET 
		Status =	CASE 
						WHEN @_TotalLine = @_TotalStatus THEN 'N'
						WHEN @_TotalLine = @_TotalStatus / 2 THEN 'C'
						ELSE 'S'
					END
	WHERE 
		InOutID = @_InOutID

	SELECT TOP 1 * FROM dbo.InOuts WHERE 1 = 2
END
GO